package logic

import (
	"context"
	"errors"
	"order_srv/cache"
	"order_srv/models/mysql"

	"order_srv/internal/svc"
	"order_srv/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsReleaseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsReleaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsReleaseLogic {
	return &GoodsReleaseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsReleaseLogic) GoodsRelease(in *order.GoodsReleaseRequest) (*order.GoodsReleaseResponse, error) {
	// todo: add your logic here and delete this line
	goods := mysql.Goods{
		UserId:     in.UserId,
		GoodsName:  in.GoodsName,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsStock: in.GoodsStock,
		GoodsCover: in.GoodsCover,
	}
	err := goods.GoodsInfo(int64(goods.ID))
	if err != nil {
		logx.Info("商品信息不存在", err)
		return nil, errors.New("商品信息不存在")
	}
	err = goods.Create()
	if err != nil {
		logx.Info("创建商品信息错误", err)
		return nil, errors.New("创建商品信息错误")
	}
	cache.SyncGoodsStock(int64(goods.ID), in.GoodsStock)
	return &order.GoodsReleaseResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
